package com.lhkj.ct.admin.system.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.model.Option;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.validator.group.ValGroups;
import com.lhkj.ct.meta.admin.model.entity.TblMenu;
import com.lhkj.ct.meta.admin.model.vo.MenuVo;
import com.lhkj.ct.meta.admin.model.vo.RouterVo;
import com.lhkj.ct.meta.admin.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  菜单管理
 * </p>
 */
@RestController
@RequestMapping("/api/system/menu")
@ApiSupport(order = 4)
@Api(tags = "04.菜单接口")
//@RequiresRoles(value = {SystemConstants.ADMIN_ROLE_CODE})
public class SysMenuController {

    @Resource
    private SysMenuService sysMenuService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "菜单树级列表", notes = "列表查询")
    @GetMapping("tree")
    ////@RequiresPermissions(value = {"system:menu:list", "system:role:menus"}, logical = Logical.OR)
    public ActionResult<List<MenuVo>> listMenus(TblMenu menu) {
        return ActionResult.success(sysMenuService.listMenus(menu));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "菜单下拉列表")
    @GetMapping("/options")
    public ActionResult<List<Option<String>>> listMenuOptions() {
        List<Option<String>> menus = sysMenuService.listMenuOptions();
        return ActionResult.success(menus);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "用户路由列表")
    @GetMapping("/routes")
//    @RequiresAuthentication
    public ActionResult<List<RouterVo>> listRoutes() {
        List<RouterVo> routeList = sysMenuService.getRoutesByLoginUser();
        return ActionResult.success(routeList);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "菜单表单数据")
    @GetMapping("get/{id}")
//    //@RequiresPermissions(value = {"system:menu:edit", "system:menu:query"}, logical = Logical.OR)
    public ActionResult<?> get(@PathVariable String id) {
        return ActionResult.success(sysMenuService.getById(id));
    }

    @ApiOperationSupport(order = 5)
    @PostMapping("insert")
    @ApiOperation(value = "新增菜单")
    @Log(title = "菜单管理", actionType = ActionType.INSERT)
//    //@RequiresPermissions(value = "system:menu:insert")
    @Validated(ValGroups.Insert.class)
    public ActionResult<Void> insert(@Valid @RequestBody TblMenu entity) {
        sysMenuService.saveMenu(entity);
        return ActionResult.success();
    }

    @ApiOperationSupport(order = 6)
    @PutMapping("update")
    @ApiOperation(value = "编辑菜单")
    @Log(title = "菜单管理", actionType = ActionType.UPDATE)
//    //@RequiresPermissions(value = "system:menu:edit")
    @Validated(ValGroups.Update.class)
    public ActionResult<Void> update(@Valid @RequestBody TblMenu entity) {
        if (sysMenuService.saveMenu(entity) > 0) {
            return ActionResult.success();
        }
        return ActionResult.fail("编辑失败");
    }

    @ApiOperationSupport(order = 7)
    @DeleteMapping("/{id}/delete")
    @ApiOperation(value = "删除菜单")
    @Log(title = "菜单管理", actionType = ActionType.DELETE)
    ////@RequiresPermissions(value = "system:menu:delete")
    public ActionResult<Void> delete(@PathVariable String id) {
        if (sysMenuService.hasChildByMenuId(id)) {
            return ActionResult.fail("存在子菜单,不允许删除");
        }
        if (sysMenuService.checkMenuExistRole(id)) {
            return ActionResult.fail("菜单已分配,不允许删除");
        }
        if (sysMenuService.removeById(id) > 0) {
            return ActionResult.success("删除成功");
        }
        return ActionResult.fail("删除失败");
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "修改菜单显示状态")
    @PatchMapping("/{menuId}/visible")
    @Log(title = "菜单管理", actionType = ActionType.UPDATE)
    public ActionResult<Void> updateMenuVisible(@PathVariable String menuId, Integer visible) {
        if (sysMenuService.updateMenuVisible(menuId, visible)) {
            return ActionResult.success();
        }
        return ActionResult.fail("操作失败");
    }
}
